package com.qiyi.player.loader.vod
{
   import flash.events.EventDispatcher;
   import flash.net.URLLoader;
   import flash.system.ApplicationDomain;
   import flash.events.Event;
   import flash.system.Security;
   import flash.net.URLLoaderDataFormat;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import flash.display.Loader;
   import flash.system.LoaderContext;
   import flash.utils.ByteArray;
   import flash.utils.clearTimeout;
   import flash.utils.setTimeout;
   import flash.net.sendToURL;
   import flash.net.NetStream;
   
   public class P2PFileLoader extends EventDispatcher
   {
      
      public function P2PFileLoader(param1:SingletonClass) {
         super();
      }
      
      public static const LostCorePingBack:String = "http://msg.video.qiyi.com/vodpb.gif?tag=lostCore&curl=";
      
      public static const Evt_LoadDone:String = "Evt_LoadDone";
      
      public static const Evt_LoadError:String = "Evt_LoadError";
      
      private static var _instance:P2PFileLoader;
      
      public static function get instance() : P2PFileLoader {
         if(_instance == null)
         {
            _instance = new P2PFileLoader(new SingletonClass());
         }
         return _instance;
      }
      
      private var _loader:URLLoader;
      
      private var _loadDone:Boolean = false;
      
      private var _loadErr:Boolean = false;
      
      private var _isLoading:Boolean = false;
      
      private var _retryMaxCount:int = 3;
      
      private var _retryCount:int = 0;
      
      private var _retryTimeout:uint;
      
      private var _url:String = "http://dispatcher.video.qiyi.com/dispn/flashppdp.swf";
      
      private var _version:String = "";
      
      private var _instanceClass:Class;
      
      private var _domain:ApplicationDomain;
      
      public function loadCore(param1:String = "") : void {
         var var_1:String = param1;
         if(this._loadDone)
         {
            dispatchEvent(new Event(Evt_LoadDone));
            return;
         }
         if(this._domain)
         {
            try
            {
               this._instanceClass = this._domain.getDefinition("com.qiyi.p2p.vod.fileMode.P2PFile") as Class;
            }
            catch(name_1:*)
            {
            }
         }
         if(this._instanceClass)
         {
            this._loadDone = true;
            this._version = this._instanceClass["p2pVersion"];
            dispatchEvent(new Event(Evt_LoadDone));
            return;
         }
         if(var_1 != "")
         {
            this._url = var_1;
         }
         Security.allowDomain("*");
         this._loader = new URLLoader();
         this._loader.dataFormat = URLLoaderDataFormat.BINARY;
         this._loader.addEventListener(Event.COMPLETE,this.onCoreComplete);
         this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onSecurityError);
         this._loader.load(new URLRequest(this._url));
         this._isLoading = true;
      }
      
      private function onCoreComplete(param1:Event) : void {
         var swfLoader:Loader = null;
         var done:Function = null;
         var var_2:Event = param1;
         done = function(param1:Event):void
         {
            _loadDone = true;
            _isLoading = false;
            swfLoader.removeEventListener(Event.COMPLETE,done);
            _instanceClass = _domain.getDefinition("com.qiyi.p2p.vod.fileMode.P2PFile") as Class;
            _version = _instanceClass["p2pVersion"];
            dispatchEvent(new Event(Evt_LoadDone));
         };
         swfLoader = new Loader();
         swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,done);
         this._domain = new ApplicationDomain();
         var context:LoaderContext = new LoaderContext(false,this._domain);
         swfLoader.loadBytes(this._loader.data as ByteArray,context);
      }
      
      private function onIOError(param1:IOErrorEvent) : void {
         clearTimeout(this._retryTimeout);
         this._retryTimeout = setTimeout(this.retry,2000);
      }
      
      private function onSecurityError(param1:SecurityErrorEvent) : void {
         clearTimeout(this._retryTimeout);
         this._retryTimeout = setTimeout(this.retry,2000);
      }
      
      private function retry() : void {
         var _loc1_:URLRequest = null;
         clearTimeout(this._retryTimeout);
         this._retryCount++;
         if(this._retryCount > this._retryMaxCount)
         {
            _loc1_ = new URLRequest();
            _loc1_.url = LostCorePingBack + this._url + "&ran=" + Math.random();
            sendToURL(_loc1_);
            this._loadErr = true;
            this._isLoading = false;
            this._loadDone = false;
            dispatchEvent(new Event(Evt_LoadError));
            return;
         }
         this._loader.load(new URLRequest(this._url + "?rn=" + Math.random()));
      }
      
      public function getFile(param1:Object, param2:NetStream) : Object {
         if(!this._loadDone)
         {
            return null;
         }
         return new this._instanceClass(param1,param2);
      }
      
      public function get version() : String {
         return this._version;
      }
      
      public function get loadDone() : Boolean {
         return this._loadDone;
      }
      
      public function get loadErr() : Boolean {
         return this._loadErr;
      }
      
      public function get isLoading() : Boolean {
         return this._isLoading;
      }
   }
}
class SingletonClass extends Object
{
   
   function SingletonClass() {
      super();
   }
}
